// 公共数据
import { defineStore } from 'pinia'
import { useRoute } from 'vue-router'
import {
  getGameTotal,
  getCurrencyTotal,
  getOperatorTotal,
  getAgentTotal,
  getLanguageList,
} from '@/api/common'
import { getExchangeList, getGlobalServerName } from '@/api/config'
import { GameBrandNum } from '@/config/eunm'
import { selectToLabel,setObjectArray } from '@/utils/index'
interface DataType {
  value: number
  label: string,
  type?:number
  remark?:string
  platform?:number
  status?:number
  currency?:number
  source?:number
}
export const useStore = defineStore({
  id: 'useStore',
  state: () => {
    return {
      path: '', // 页面路径
      loading: false, // 页面加载
      moduleLoading: false, // 组件加载
      offset: 0, // 时区偏移量
      is_general_agent: false, // 判断是否是总代登录 判断是否显示商户类别
      BackOperator: 'OMG', // 判断后台品牌显示【OMG-MG】
      BackServer: '美服', // 判断后台服务器显示【美服-亚服】
      breadArray: [] as any[], // 顶部菜单导航栏
      pathArray: [] as any[], // 菜单栏
      userInfo: {} as any, // 用户信息
      queryArray: [] as any[], // 页面缓存查询信息

      operatorArray: [] as Array<DataType>, // 运营商汇总
      gameArray: [] as Array<DataType>, // 游戏汇总
      agentArray: [] as Array<DataType>, // 商户汇总
      platformArray: [] as Array<DataType>, // 游戏品牌汇总
      currentArray: [] as Array<DataType>, // 货币汇总
      langList: [] as Array<DataType>, // 语言列表
      exchangeList: [] as Array<DataType>, // 货币汇率汇总
      serverNameList: [] as Array<DataType>, // 全球服名称
    }
  },
  getters: {},
  actions: {
    // 设置时间范围不能选择之后的时间 :disabled-date="indexStore.disabledDateTime"
    disabledDateTime(time:any) {
      return time.getTime() >= Date.now() - 86400; //代表一天(s)
    },
    disabledBeforeDateTime(time:any) {
      return time.getTime() + 86400000 < Date.now(); //代表一天(ms)
    },
    // 设置路由路径
    setLocalPath(path: string) {
      this.path = path
    },
    // 设置页面加载
    setPageLoading(loading: boolean) {
      this.loading = loading
    },
    // 设置组件加载
    setModuleLoading(moduleLoading: boolean) {
      this.moduleLoading = moduleLoading
    },
    // 设置时区偏移量
    setOffset(offset: number) {
      this.offset = offset
    },
    // 设置导航菜单栏
    setLocalBread(breadArray: any) {
      this.breadArray = breadArray
    },
    // 设置用户信息
    setUserInfo(userInfo: any) {
      this.userInfo = userInfo
    },
    // 存取页面查询条件 get是取 set是存
    setQueryCache(query:any,type:string) {
      const route = useRoute()
      if(type=='get'){
        if(selectToLabel(this.queryArray,route.name)){
          return selectToLabel(this.queryArray,route.name)
        }else{
          return query
        }
      }else{
        if(selectToLabel(this.queryArray,route.name)){
          this.queryArray.forEach((item:any)=>{
            if(route.name == item.value){
              item.label = query
            }
          })
        }else{
          this.queryArray.push({label:query,value:route.name})
        }
      }
    },
    // 获取游戏汇总
    async getGame() {
      const res = await getGameTotal()
      const { data } = res
      this.gameArray = data?.sort(function(a, b){
        return a.value - b.value
      })
      let platformData  = [] as any[],newData=[] as any[];
      this.platformArray= []
      data?.forEach((item:any)=>{
        platformData.push({ platform:item.platform,game_source:item.game_source })
      })
      newData = setObjectArray(platformData,'platform')
      newData.forEach((item:any)=> {
        // 为了排除已经合并的品牌
        if(selectToLabel(GameBrandNum,item.platform)){
          this.platformArray.push({label:selectToLabel(GameBrandNum,item.platform),value:item.platform,status:1,source:item.game_source })
        }
      })
    },
    // 获取货币汇总
    async getCurrent() {
      const res = await getCurrencyTotal()
      const { data } = res
      this.currentArray = data
    },
    // 获取总代汇总
    async getOperator() {
      const res = await getOperatorTotal()
      const { data } = res
      this.operatorArray = data
    },
    // 获取商户汇总
    async getAgent() {
      const res = await getAgentTotal()
      const { data } = res
      this.agentArray = data
    },
    // 语言列表
    async getLanguageList() {
      const res = await getLanguageList()
      const { data } = res
      this.langList = data
    },
    // 获取货币汇率汇总
    async getExchange() {
      const res = await getExchangeList({page:1,size:999})
      const { data } = res
      this.exchangeList = []
      data.list?.forEach((item:any)=> {
        this.exchangeList.push({ label: item.name, value: item.currency_id, type: item.symbol, status:item.usdt_exchange_rate})
      })
    },
    // 获取全球服名称
    async getServerName() {
      const res = await getGlobalServerName({server_type:2})
      const { data } = res
      this.serverNameList = []
      data.forEach((item:any)=> {
        this.serverNameList.push({ label: item.server_name, value: item.server_id})
      })
    },
  }
})
